package com.medical.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.config.SystemContext;
import com.medical.dao.SysYljgMapper;
import com.medical.entity.SysYljg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class YljgService {
    @Autowired
    private SysYljgMapper sysYljgMapper;


    /**
     * 根据用户ID查找用户信息
     * @param yljgId
     * @return
     */
    public SysYljg findYljgByYljgId(Integer yljgId) {
        return sysYljgMapper.findYljgByYljgId(yljgId);
    }

    /**
     * 查找用户列表
     * @return
     */
    public PageInfo<SysYljg> getYljgList(String yljgName, Integer currentPage, Integer limit){
        //利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysYljg> yljgList = sysYljgMapper.getYljgList(yljgName);
        PageInfo<SysYljg> pageInfo = new PageInfo<SysYljg>(yljgList);
        return pageInfo;
    }

    /**
     * 根据ID获取用户信息
     * @param id
     * @return
     */
    public SysYljg getYljgById(Integer id) {
        return sysYljgMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加
     * @param sysYljg
     */
    public void addYljg(SysYljg sysYljg) {
        sysYljg.setCreateTime(new Timestamp(System.currentTimeMillis()));
        sysYljg.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        sysYljg.setyljgOther(SystemContext.passwdEncryption(sysYljg.getyljgOther()));

        sysYljgMapper.insert(sysYljg);
    }

    /**
     * 更新
     * @param sysYljg
     */
    public void edit(SysYljg sysYljg) {
        sysYljg.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        sysYljgMapper.updateByPrimaryKeySelective(sysYljg);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id) {
        sysYljgMapper.deleteByPrimaryKey(id);
    }
}
